SD Card Interfacing with ESP32

In embedded systems, time information is monitored through logging and data storage. Most microcontrollers, including ESP32, can store sensor readings, logs, or configuration files on SD cards. In this tutorial, we will explain how to interface an SD card with the ESP32 microcontroller and display the card’s status along with volume on a 20×4 I2C LCD. We will then create a text file on the SD card and write some data.

components of esp32 and SD card Interfacing

Figure 1: Components of esp32 and SD card Interfacing

Hardware Components Required

  1. ESP32 development board
  2. SD card module
  3. SD card
  4. 20×4 I2C LCD

Libraries Required:

  1. SD.h
  2. Wire
  3. LiquidCrystal_I2C

Preparing SD Card

Before interfacing the SD card with ESP32, it is essential to ensure the card is formatted as FAT32. This ensures that the card can be read and written using the ESP32’s file system library.

Formatting the SD Card

  1. Place the SD card in your computer via an SD card reader.
  2. Right-click on SD Card and click on Format.
    sd card format menuFigure 2: SD Card Format Menu
  3. A window for formatting will appear there. Select FAT32 and click on start.
    sd card format in fat32Figure 3: SD Card Format in FAT32
  4. After Format, a window will appear with the message “Format complete.”
    SD card Format CompleteFigure 4: SD Card Format Complete

 

Circuit Diagram SD Card Interfacing with ESP32

The connections should not be made without ensuring that the logic levels of the SD card module and the I2C LCD are compatible with the ESP32, which operates at 3.3V.

Connection of ESP32 with SD card

S.N. SD Card Module Pins ESP32 Pins
1 CS (Chip Select) GPIO 5
2 MOSI (Master Out Slave In) GPIO 23
3 MISO (Master In Slave Out) GPIO 19
4 CLK (Clock) GPIO 18
5 Vcc Vin
6 GND GND

esp32 and sd card interface

Figure 5: Circuit Diagram SD Card Interfacing with ESP32

Software Code:

Code Explanation

The program will:

Initialize the SD card: It checks whether or not it is correctly inserted and if it is accessible.

Display “SD Card Present” or “SD Card Failed” to indicate the card’s presence on the LCD.

Show the SD card’s volume: Display the SD card’s total size.

This sketch will create a text file on the SD card and write data.

Creating the File and Writing on It: Once the SD card is initialized successfully, a file named bestengineeringprojects.txt is created (opened if it exists) by SD.open() The URL www.bestengineeringprojects.com is written to the file.

The file is then closed to save the data written in it properly.

Error Handling:

If the file is not opened, the LCD will display “File Write Error,” sending an error message regarding file manipulation.

Show Status of the Write:

If the file were successfully written, the LCD would display “File Written OK.”

ESP32 and SD card Interfacing with LCD

Figure 6: Author Prototype of ESP32 and SD Card Interfacing

Troubleshooting and Testing

SD Card Initialization Failed:

  1. Check if the connections are made correctly.
  2. Whether the formatting of the SD card has been done with FAT32.
  3. Check wiring for appropriate voltage level.

I2C LCD Not Operating:

  1. Recheck the LCD’s I2C address; if necessary, use an I2C scanner to find the correct address.
  2. Check connections between the SDA and SCL of the LCD for tightness.

Conclusion

In this tutorial, we have interfaced an SD card with the ESP32 microcontroller and displayed its status and volume information on a 20×4 I2C LCD. Furthermore, we have created a text file and written data in it. Proper SD card formatting beforehand will ensure that the file system operations run smoothly. This can be a good starting point for data logging and embedded file management projects.

Leave a Comment